import traceback

from selenium import webdriver
import os
import time
import re
from bs4 import BeautifulSoup
import random

from selenium.webdriver.common.by import By
import mysql_connect


class ZhihuQuestion():
    def __init__(self, url, title, display_text, zhihu_data_id):
        self.url = url
        self.title = title
        self.display_text = display_text
        self.zhihu_data_id = zhihu_data_id

    def __str__(self):
        return f"ZhihuQuestion(url={self.url}, title={self.title}, display_text={self.display_text}), zhihu_data_id={self.zhihu_data_id}"

    def __repr__(self):
        return f"ZhihuQuestion(url={self.url}, title={self.title}, display_text={self.display_text}), zhihu_data_id={self.zhihu_data_id}"


class ZhihuEssay():
    def __init__(self, url, title, display_text, zhihu_data_id):
        self.url = url
        self.title = title
        self.display_text = display_text
        self.zhihu_data_id = zhihu_data_id

    def __str__(self):
        return f"ZhihuEssay(url={self.url}, title={self.title}, display_text={self.display_text}), zhihu_data_id={self.zhihu_data_id}"

    def __repr__(self):
        return f"ZhihuEssay(url={self.url}, title={self.title}, display_text={self.display_text}), zhihu_data_id={self.zhihu_data_id}"


def get_data():
    zhihu_question_dict_all_list = []
    zhihu_essay_dict_all_list = []
    zhihu_question_all_list: list[ZhihuQuestion] = []
    zhihu_essay_all_list: list[ZhihuEssay] = []

    with open(f"other_files/question_info/question_info.csv", "r", encoding="utf-8") as question_file:
        zhihu_question_dict_all_list = eval(question_file.read())
    zhihu_question_all_list = [ZhihuQuestion(**question) for question in zhihu_question_dict_all_list]
    print("读取question完毕")

    with open(f"other_files/essay_info/essay_info.csv", "r", encoding="utf-8") as essay_file:
        zhihu_essay_dict_all_list = eval(essay_file.read())
    zhihu_essay_all_list = [ ZhihuEssay(**essay) for essay in zhihu_essay_dict_all_list ]
    print("读取essay完毕")
    return zhihu_question_all_list, zhihu_essay_all_list

def question_save_in_mysql(zhihu_question_all_list: list[ZhihuQuestion]):
    insert_list = [ ( question.url, question.title, question.zhihu_data_id) for question in zhihu_question_all_list ]
    conn,cursor = mysql_connect.get_conn()
    try:
        sql = f"""
                INSERT INTO zhihu_question ( url, title, zhihu_data_id )
                    VALUES
        	        ( %s, %s, %s );
            """

        cursor.executemany(sql, insert_list)
        conn.commit()

    except:
        traceback.print_exc()
    finally:
        cursor.close()
        conn.close()

def essay_save_in_mysql(zhihu_essay_all_list: list[ZhihuEssay]):
    insert_list = [ ( essay.url, essay.title, essay.zhihu_data_id) for essay in zhihu_essay_all_list ]
    conn,cursor = mysql_connect.get_conn()
    try:
        sql = f"""
                INSERT INTO zhihu_essay ( url, title, zhihu_data_id )
                    VALUES
        	        ( %s, %s, %s );
            """

        cursor.executemany(sql, insert_list)
        conn.commit()

    except:
        traceback.print_exc()
    finally:
        cursor.close()
        conn.close()

def main():
    zhihu_question_all_list, zhihu_essay_all_list = get_data()
    question_save_in_mysql(zhihu_question_all_list)
    essay_save_in_mysql(zhihu_essay_all_list)

if __name__ == "__main__":
    main()
